package assetservicelogic

import (
	"context"
	"property_service/dao/model"
	"property_service/utils/BeanUtils"

	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
)

type AccountBindingPageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAccountBindingPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AccountBindingPageLogic {
	return &AccountBindingPageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 分页查询
func (l *AccountBindingPageLogic) AccountBindingPage(in *property_service.AccountBindingPageReq) (*property_service.AccountBindingPageRes, error) {
	var data []*model.AccountBinding
	m := query.Use(l.svcCtx.Gorm).AccountBinding
	do := m.WithContext(l.ctx).Where(m.TenantID.Eq(in.Where.TenantID))

	if in.Where.ID > 0 {
		do = do.Where(m.ID.Eq(in.Where.ID))
	} else {
		// 字符模糊查询
		if in.Where.Code != "" {
			do = do.Where(m.Code.Like("%" + in.Where.Code + "%"))
		}
		if in.Where.Name != "" {
			do = do.Where(m.Name.Like("%" + in.Where.Name + "%"))
		}
		if in.Where.ClassificationCode != "" {
			do = do.Where(m.ClassificationCode.Like("%" + in.Where.ClassificationCode + "%"))
		}
		if in.Where.ClassificationName != "" {
			do = do.Where(m.ClassificationName.Like("%" + in.Where.ClassificationName + "%"))
		}
		if in.Where.SubjectCode1 != "" {
			do = do.Where(m.SubjectCode1.Like("%" + in.Where.SubjectCode1 + "%"))
		}
		if in.Where.SubjectName1 != "" {
			do = do.Where(m.SubjectName1.Like("%" + in.Where.SubjectName1 + "%"))
		}
		if in.Where.SubjectCode2 != "" {
			do = do.Where(m.SubjectCode2.Like("%" + in.Where.SubjectCode2 + "%"))
		}
		if in.Where.SubjectName2 != "" {
			do = do.Where(m.SubjectName2.Like("%" + in.Where.SubjectName2 + "%"))
		}
		if in.Where.SubjectCode3 != "" {
			do = do.Where(m.SubjectCode3.Like("%" + in.Where.SubjectCode3 + "%"))
		}
		if in.Where.SubjectName3 != "" {
			do = do.Where(m.SubjectName3.Like("%" + in.Where.SubjectName3 + "%"))
		}
		if in.Where.Remark != "" {
			do = do.Where(m.Remark.Like("%" + in.Where.Remark + "%"))
		}
	}

	var count int64
	if in.Page+in.Limit <= 1 {
		data, _ = do.Find()
		count = int64(len(data))
	} else {
		data, count, _ = do.FindByPage(int((in.Page-1)*in.Limit), int(in.Limit))
	}

	list, _ := BeanUtils.CopyProperties(data, property_service.AccountBinding{})
	return &property_service.AccountBindingPageRes{Count: count, List: list.([]*property_service.AccountBinding)}, nil
}
